package com.ycsj.goldmedalnewconcept.common.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.PopUpManager;
import com.ycsj.common.view.dialog.AlertDialogUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.activity.WordsPrepareActivity;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.MilkUtils;

import org.greenrobot.eventbus.EventBus;


/**
 * 词汇预习完成后最后一个空页面
 */
public class EmptyFragment extends BaseLazyFragment {

    //教师账号,班级号,单元号
    private String tel, classNum, lessonid;

    /**
     * @param lessonid 单元号
     * @return
     */
    public static EmptyFragment newInstance(String lessonid) {
        EmptyFragment fragment = new EmptyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("lessonid", lessonid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_empty;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //获取传递的数据
        initIntent();
    }

    /**
     * 获取传递的数据
     */
    private void initIntent() {
        Bundle bundle = getArguments();
        lessonid = bundle.getString("lessonid");

        classNum = AccountUtils.getClassNum(getContext());
        tel = AccountUtils.getTeacher(getContext());

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
//            NormalDialogCustomAttr();

            showAgPop();
        }
    }

    /**
     * 弹出跳转的Dialog
     */
    private void NormalDialogCustomAttr() {

        AlertDialogUtil.showAlertDialogYuxi(true, getContext(), "预习完毕是否重来", new AlertDialogUtil.onListener() {
            @Override
            public void onClickOk() {

                if ("0".equals(AccountUtils.getRole(getContext()))) {
                    //词汇预习  source来源编号是3,moduleid模块编号是3,score为空
                    MilkUtils.getMilk(getContext(), tel, classNum, "3", lessonid, "3", "");
                }

                // 发送关闭宿主Activity
                getActivity().finish();

            }

            @Override
            public void onClickDismiss() {
                if ("0".equals(AccountUtils.getRole(getContext()))) {
                    //词汇预习  source来源编号是3,moduleid模块编号是3,score为空
                    MilkUtils.getMilk(getContext(), tel, classNum, "3", lessonid, "3", "");
                }

                // 发送ViewPager跳转到第一页的广播
                BusInfo busInfo = new BusInfo();
                busInfo.type = WordsPrepareActivity.GOONE;
                EventBus.getDefault().post(busInfo);

            }
        });
    }


    /**
     * 弹出跳转的PopUpWindow
     */
    private void showAgPop() {
        PopUpManager.showPopByMatchParent(getContext(), R.layout.pop_go_back, 0.3f, getActivity(), new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                TextView tv_again = view.findViewById(R.id.tv_again);
                TextView tv_over = view.findViewById(R.id.tv_over);
                tv_again.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //再来一次
                        if (!isDoubleClick(v)) {

                            if ("0".equals(AccountUtils.getRole(getContext()))) {
                                //词汇预习  source来源编号是3,moduleid模块编号是3,score为空
                                MilkUtils.getMilk(getContext(), tel, classNum, "3", lessonid, "3", "");
                            }

                            // 发送关闭宿主Activity
                            getActivity().finish();
                        }

                    }
                });

                tv_over.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //结束预习
                        if (!isDoubleClick(v)) {
                            if ("0".equals(AccountUtils.getRole(getContext()))) {
                                //词汇预习  source来源编号是3,moduleid模块编号是3,score为空
                                MilkUtils.getMilk(getContext(), tel, classNum, "3", lessonid, "3", "");
                            }


                            //关闭弹框
                            PopUpManager.dismiss();

                            // 发送ViewPager跳转到第一页的广播
                            BusInfo busInfo = new BusInfo();
                            busInfo.type = WordsPrepareActivity.GOONE;
                            EventBus.getDefault().post(busInfo);

                        }
                    }
                });

            }
        });
    }
}
